# Contributor: TBK <alpine@jjtc.eu>
# Maintainer: Ariadne Conill <ariadne@dereferenced.org>
pkgname=libretls
pkgver=3.7.0
pkgrel=2
pkgdesc="port of libtls from libressl to openssl"
arch="all"
url="https://git.causal.agency/libretls/"
license="ISC AND ( BSD-3-Clause OR MIT )"
depends="ca-certificates-bundle"
depends_dev="openssl-dev>3"
base_deps="autoconf automake libtool"
makedepends="$depends_dev $base_deps"
makedepends_host="openssl-dev>3"
makedepends_build="$base_deps"

subpackages="$pkgname-doc $pkgname-static $pkgname-dev"
source="https://causal.agency/libretls/libretls-$pkgver.tar.gz
	test_program.c"

# secfixes:
#   3.5.1-r0:
#     - CVE-2022-0778

prepare() {
	default_prepare
	update_config_sub

	# Override the LIBTLS_VERSION to be 2:*:*, as libtls-standalone was 1:0:0.
	# also make sure the LIBTLS version is different from upstream libressl
	# so they can be installed in parallel
	local _current _rev _age
	IFS=: read _current _rev _age < LIBTLS_VERSION
	printf "%d:%d:%d\n" $(( $_current - 18 )) $_rev $_age > LIBTLS_VERSION

	autoreconf
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr
	make all
}

package() {
	make DESTDIR="$pkgdir" install
}

check() {
	set -x

	# shellcheck disable=SC2046
	gcc -o test_program "$srcdir"/test_program.c -I"$builddir"/include -L"$builddir"/.libs -ltls $(pkgconf --cflags --libs openssl)
	LD_LIBRARY_PATH="$builddir"/.libs ./test_program

	set +x
}

sha512sums="
18987d09ee804883e2c3cfae1209494b7020d976cc7137ba39540c18e0dc23d25034a46c6c64955201b425ca981e03cbe629f9ff979fd4a72d3dcfbd51efdf84  libretls-3.7.0.tar.gz
71d36fe25c95a0a45497e3f699b01dddcaae9053dd1b1e2419df94272c47024cf6516c51c902129201061601b04a72551904b15a332a4cf53358983b5db73618  test_program.c
"
